package com.bingxue.edu.management.education.controller.exam;

import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.education.model.entity.exam.Exam;
import com.bingxue.edu.management.education.model.query.ExamQuery;
import com.bingxue.edu.management.education.model.req.ExamCreateReq;
import com.bingxue.edu.management.education.model.req.ExamUpdateReq;
import com.bingxue.edu.management.education.model.resp.exam.ExamQuestionResp;
import com.bingxue.edu.management.education.model.resp.exam.ExamResp;
import com.bingxue.edu.management.education.model.resp.exam.ExamScoreTableResp;
import com.bingxue.edu.management.education.service.exam.ExamExcelService;
import com.bingxue.edu.management.education.service.exam.ExamQuestionService;
import com.bingxue.edu.management.education.service.exam.ExamRecordService;
import com.bingxue.edu.management.education.service.exam.ExamService;
import com.bingxue.edu.parent.model.resp.ExamScoreDistResp;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;

import static com.bingxue.edu.management.education.model.entity.exam.table.ExamQuestionTableDef.EXAM_QUESTION;

@Slf4j
@Tag(name = "考试接口")
@RestController
@RequestMapping("/education/exam")
@RequiredArgsConstructor
public class ExamController {

    private final ExamService examService;
    private final ExamExcelService examExcelService;
    private final ExamQuestionService examQuestionService;
    private final ExamRecordService examRecordService;

    @Operation(summary = "考试成绩列表" , description = "查询考试成绩列表")
    @ApiResource("edu:exam:read")
    @GetMapping("/list")
    public Response<List<ExamResp>> examList(ExamQuery query) {
        QueryWrapper queryWrapper = QueryBuilder.build(query);
        List<ExamResp> examRespList = examService.listAs(queryWrapper, ExamResp.class);
        return Response.success(examRespList);
    }

    @Operation(summary = "查询考试成绩详情" , description = "查询考试成绩详情")
    @ApiResource("edu:exam:read")
    @GetMapping("/detail/{id}")
    public Response<ExamResp> examDetail(@PathVariable String id) {
        ExamResp resp = examService.getMapper().selectOneByIdAs(id, ExamResp.class);
        return Response.success(resp);
    }

    @Operation(summary = "新增考试成绩" , description = "新增考试成绩")
    @ApiResource("edu:exam:write")
    @PostMapping("/create")
    public Response<Void> createExam(@Validated @RequestBody ExamCreateReq req) {
        Exam exam = examService.copy(req);
        //默认赋值
        exam.setStudentCount(0);
        exam.setHighestScore(BigDecimal.ZERO);
        exam.setAverageScore(BigDecimal.ZERO);
        exam.setLowestScore(BigDecimal.ZERO);
        exam.setTotalScore(BigDecimal.ZERO);
        examService.save(exam);
        return Response.success();
    }

    @Operation(summary = "删除考试成绩" , description = "删除考试成绩")
    @ApiResource("edu:exam:write")
    @PostMapping("/delete/{ids}")
    public Response<Void> deleteExam(@PathVariable List<String> ids) {
        examService.remove(ids);
        return Response.success();
    }

    @Operation(summary = "更新数据",description = "更新数据")
    @ApiResource("edu:exam:write")
    @PostMapping("/update/{id}")
    public Response<Void> update(@RequestBody ExamUpdateReq req, @PathVariable String id){
        Exam exam = examService.copyById(req, id);
        examService.updateById(exam);
        return Response.success();
    }

    @Operation(summary = "导入考试成绩Excel表格" , description = "导入考试成绩Excel表格")
    @ApiResource("edu:exam:write")
    @PostMapping("/import")
    public Response<Void> importData(@RequestPart("file") MultipartFile file) {
        examExcelService.importData(file);
        return  Response.success();
    }

    @Operation(summary = "考试题目列表" , description = "查询考试题目列表")
    @ApiResource("edu:exam:read")
    @GetMapping("/question-list")
    public Response<List<ExamQuestionResp>> getQuestionList(@RequestParam("examId") String examId) {
        QueryWrapper examQuestionQueryWrapper = QueryWrapper.create()
                .select(EXAM_QUESTION.ALL_COLUMNS)
                .where(EXAM_QUESTION.EXAM_ID.eq(examId));
        List<ExamQuestionResp> examQuestionRespList= examQuestionService.listAs(examQuestionQueryWrapper,ExamQuestionResp.class);
        return Response.success(examQuestionRespList);
    }

    @Operation(summary = "考试成绩册" , description = "查询考试成绩册")
    @ApiResource("edu:exam:read")
    @GetMapping("/score-table")
    public Response<ExamScoreTableResp> getScoreTable(@RequestParam("examId") String examId) {
        ExamScoreTableResp scoreTable = examRecordService.getScoreTable(examId);
        return Response.success(scoreTable);
    }

    @Operation(summary = "考试成绩分布统计" , description = "查询考试成绩分布统计")
    @ApiResource("edu:exam:read")
    @GetMapping("/score-dist")
    public Response<ExamScoreDistResp> getScoreDist(@RequestParam("examId") String examId) {
        ExamScoreDistResp resp = examRecordService.getScoreDist(examId);
        return Response.success(resp);
    }
}
